﻿using System;
using System.Windows;
using Gao.Poker.App.Print;
using Gao.Poker.App.ViewModel;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.App.Game
{
    /// <summary>
    /// Interaction logic for BuyStakeWin.xaml
    /// </summary>
    public partial class BuyStakeWin : Window
    {
        public GameViewModel ViewModel { get; set; }

        private readonly MemberService ms = MemberService.Instance();
        private readonly CompetitionEntryService ces = CompetitionEntryService.Instance();

        public BuyStakeWin()
        {
            InitializeComponent();

            btnSearch.Click += Search_Click;
        }

        void Search_Click(object sender, RoutedEventArgs e)
        {
            MemberService service = new MemberService();
            var list = service.SearchMembers(txtName.Text, txtCardNo.Text);

            dgMembers.ItemsSource = list;
        }

        private void Bubmit_Click(object sender, RoutedEventArgs e)
        {
            Gao.Poker.Domain.Member member = dgMembers.SelectedItem as Gao.Poker.Domain.Member;
            if (member == null)
            {
                MessageBox.Show("请选择报名的会员", "提示信息");
                return;
            }

            member = ms.GetMember(member.CardNo);

            if(iuSeveral.Value == null)
            {
                MessageBox.Show("不能买入0份", "提示信息");
                return;
            }

            
            var competitionEntry = ces.GetMember(member.CardNo, ViewModel.Competition.ID);
            if(competitionEntry == null)
            {
                MessageBox.Show("当前会员未报名参加比赛", "提示信息");
                return;
            }

            BuyRecord record = new BuyRecord();
           
            if (rbBuyAgain.IsChecked == true)
            {
                if (member.Points < iuSeveral.Value.Value * ViewModel.Competition.SinglePoints)
                {
                    MessageBox.Show("当前积分不足以购买", "提示信息");
                    return;
                }

                record.BuyType = 1;
                competitionEntry.BuyAgainNumber += iuSeveral.Value.Value;
                competitionEntry.BuyMoney += iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;
                ViewModel.Competition.BuyAgainNumber += iuSeveral.Value.Value;
                ViewModel.Competition.TotalBet += iuSeveral.Value.Value * ViewModel.Competition.Chips;
                ViewModel.Competition.TotalPoints += iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;

                member.Points -= iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;
            }
            else if(rbBuyLast.IsChecked == true)
            {

                if (member.Points < iuSeveral.Value.Value * ViewModel.Competition.SinglePoints)
                {
                    MessageBox.Show("当前积分不足以购买", "提示信息");
                    return;
                }

                record.BuyType = 2;
                competitionEntry.BuyLastNumber += iuSeveral.Value.Value;
                competitionEntry.BuyMoney += iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;
                ViewModel.Competition.BuyLastNumber += iuSeveral.Value.Value;
                ViewModel.Competition.TotalBet += iuSeveral.Value.Value * ViewModel.Competition.Chips * 2;
                ViewModel.Competition.TotalPoints += iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;

                member.Points -= iuSeveral.Value.Value * ViewModel.Competition.SinglePoints;
            }

            record.CardNo = member.CardNo;
            record.ConsumptionID = ViewModel.Competition.ID;
            record.Several = iuSeveral.Value.Value;
            record.CreateDt = DateTime.Now;
           
            BuyRecordService service = new BuyRecordService();
            service.Save(record);
            ViewModel.Competition = CompetitionService.Instance().Save(ViewModel.Competition);

            ces.Save(competitionEntry);

            ms.Save(member);

            //拿到座位号，打印
            var seats = ViewModel.Members.Find(a=>a.CardNo.Equals(member.CardNo));

            String seatNum = seats != null ? seats.SeatNum : String.Empty;

            //CompetitionPrint print = new CompetitionPrint();
            PrintHelper.Instance().CompetitionPrint(member.Name, member.CardNo,
                                                    ViewModel.Competition.Name,
                                                    rbBuyAgain.IsChecked == true ? "Rebuy" : "Addon",
                                                    (iuSeveral.Value.Value*ViewModel.Competition.SinglePoints).ToString(),
                                                    member.Points.ToString(), seatNum);

            //PrintDialog dialog = new PrintDialog();

            //if (dialog.ShowDialog() == true)
            //{
            //    dialog.PrintVisual(print, "Competition Print");
            //}
            

            ViewModel.OnPropertyChanged("Members");
            ViewModel.OnPropertyChanged("Competition");
            Close();
        }
    }
}
